package com.yjh.auth.wechat;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.yjh.auth.common.exception.ResourceAuthExceptionEntryPoint;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationEventPublisher;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import org.springframework.stereotype.Component;


/**
 * smithGuo
 * 微信登录配置入口
 */
@Getter
@Setter
@Component
public class WechatSecurityConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {
    @Autowired
    private ObjectMapper objectMapper;
    @Getter
    @Setter
    private AuthenticationEventPublisher defaultAuthenticationEventPublisher;
    @Autowired
    private AuthenticationSuccessHandler wechatLoginSuccessHandler;


    @Override
    public void configure(HttpSecurity http) {
        WechatAuthenticationFilter wechatAuthenticationFilter = new WechatAuthenticationFilter();
        wechatAuthenticationFilter.setAuthenticationManager(http.getSharedObject(AuthenticationManager.class));
        wechatAuthenticationFilter.setAuthenticationSuccessHandler(wechatLoginSuccessHandler);//微信授权登录
        wechatAuthenticationFilter.setEventPublisher(defaultAuthenticationEventPublisher);
        wechatAuthenticationFilter.setAuthenticationEntryPoint(new ResourceAuthExceptionEntryPoint(objectMapper));

//        WechatAuthenticationProvider wechatAuthenticationProvider = new WechatAuthenticationProvider();
//        wechatAuthenticationProvider.setUserDetailsService(userDetailsService);
        http.addFilterAfter(wechatAuthenticationFilter, UsernamePasswordAuthenticationFilter.class);


    }

}
